import { observable, action } from 'mobx';
import * as Apis from "../../api/customerApis";
import { ListViewPage,RouteLayout,MapPage  } from 'weaver-mobile-page';

export default class CustomerMapStore {
    comObj = {};
    params = {};
    @observable lat1 = 0;
    @observable lng1 = 0;
    @observable customerId = "";
    @observable customerName = "";
    @observable customerAddress = "";
    @observable raidus = 1;
    @observable customerListNormalStore = new ListViewPage.Store({ route: '/customerListNormal',id:"customerListNormal" });
    @observable initResponsed = false;// 是否已经得到初始化数据的响应
    @observable initSuccessed = false;// 初始化数据是否获取成功
    @observable initFailedMsg = "";   // 初始化处理失败的原因
    @observable customerCardLayout = {};
    @observable mapPage = new MapPage.Store({ route: '/aroundCustomers',id:"aroundCustomers" });

    @action
    doNew = (comObj,params) =>{
      this.comObj = comObj;
      this.params = params;
    }

    @action
    doInit = () =>{
      let params = {id:this.params.customerId};
      Apis.getCustomerCard(params).then(data => {
        this.doInitCallBack(params,data);
      });
    }

    @action
    doInitCallBack = (params,data) =>{
      this.initResponsed = true;
      if(data.status == "0"){
        this.initSuccessed = false;
        this.initFailedMsg = data.errorMsg;
      }else{
        const currentPath = this.comObj.props.location.pathname;
        this.customerCardLayout = new RouteLayout.Store({route:currentPath , id:'customerCardLayout'});
        this.initSuccessed = true;
        this.customerId = data.customerId;
        this.customerName = data.customerName;
        this.customerAddress = data.customerAddress;
        this.fixedPosition = data.fixedPosition;
        this.lat1 = data.lat1 ==""?0:parseFloat(data.lat1);
        this.lng1 = data.lng1==""?0:parseFloat(data.lng1);
      }
    }

    @action
    doClear = () =>{
      this.lat1 = 0;
      this.lng1 = 0;
      this.customerId = "";
      this.customerName = "";
      this.customerAddress = "";
      this.raidus = 1;
      this.initResponsed = false;
      this.initSuccessed = false;
      this.initFailedMsg = "";      
    }

    @action
    getCustomerList(){
      const params = {opt:"around",aroundCrm:this.customerId,raidus:this.raidus,lat:this.lat1,lng:this.lng1};
      Apis.getCustomerListResult(params).then(data => { 
        const dataKey = data.datas;
        this.customerListNormalStore.clearData();
        this.customerListNormalStore.getData({ dataKey });
      });
    }
  
    @action
    setRaidus(rds){
      this.raidus = rds;
      this.getCustomerList();
    }
}